<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html id="htmlId">
<head>
    <title>Coverage Report :: JPacmanFuzzer</title>
    <style type="text/css">
        @import "../../.css/coverage.css";
    </style>
</head>

<body>
<div class="header"></div>

<div class="content">
    <div class="breadCrumbs">
        [ <a href="../../index.html">all classes</a> ]
        [ <a href="../index.html">nl.tudelft.jpacman.fuzzer</a> ]
    </div>

    <h1>Coverage Summary for Class: JPacmanFuzzer (nl.tudelft.jpacman.fuzzer)</h1>

    <table class="coverageStats">
        <tr>
            <th class="name">Class</th>
            <th class="coverageStat
">
                Class, %
            </th>
            <th class="coverageStat
">
                Method, %
            </th>
            <th class="coverageStat
">
                Line, %
            </th>
        </tr>
        <tr>
            <td class="name">JPacmanFuzzer</td>
            <td class="coverageStat">
  <span class="percent">
    0%
  </span>
                <span class="absValue">
    (0/ 1)
  </span>
            </td>
            <td class="coverageStat">
  <span class="percent">
    0%
  </span>
                <span class="absValue">
    (0/ 7)
  </span>
            </td>
            <td class="coverageStat">
  <span class="percent">
    0%
  </span>
                <span class="absValue">
    (0/ 33)
  </span>
            </td>
        </tr>

    </table>

    <br/>
    <br/>


    <div class="sourceCode"><i>1</i>&nbsp;package nl.tudelft.jpacman.fuzzer;
        <i>2</i>&nbsp;
        <i>3</i>&nbsp;import nl.tudelft.jpacman.Launcher;
        <i>4</i>&nbsp;import nl.tudelft.jpacman.board.Direction;
        <i>5</i>&nbsp;import nl.tudelft.jpacman.game.Game;
        <i>6</i>&nbsp;import nl.tudelft.jpacman.level.Player;
        <i>7</i>&nbsp;import org.junit.jupiter.api.AfterEach;
        <i>8</i>&nbsp;import org.junit.jupiter.api.BeforeAll;
        <i>9</i>&nbsp;import org.junit.jupiter.api.BeforeEach;
        <i>10</i>&nbsp;import org.junit.jupiter.api.Disabled;
        <i>11</i>&nbsp;import org.junit.jupiter.api.RepeatedTest;
        <i>12</i>&nbsp;import org.junit.jupiter.api.RepetitionInfo;
        <i>13</i>&nbsp;
        <i>14</i>&nbsp;import java.io.BufferedWriter;
        <i>15</i>&nbsp;import java.io.File;
        <i>16</i>&nbsp;import java.io.FileOutputStream;
        <i>17</i>&nbsp;import java.io.IOException;
        <i>18</i>&nbsp;import java.io.OutputStreamWriter;
        <i>19</i>&nbsp;import java.nio.charset.StandardCharsets;
        <i>20</i>&nbsp;import java.util.Random;
        <i>21</i>&nbsp;
        <i>22</i>&nbsp;/**
        <i>23</i>&nbsp; * A basic fuzzer trying out random moves and writing logs in output files. Number of runs is a
        <i>24</i>&nbsp; * tunable parameter. Each run initiates the game and is played until PacMan dies.
        <i>25</i>&nbsp; *
        <i>26</i>&nbsp; * @author Azqa Nadeem
        <i>27</i>&nbsp; */
        <i>28</i>&nbsp;@Disabled
        <b class="nc"><i>29</i>&nbsp;class JPacmanFuzzer {</b>
        <i>30</i>&nbsp; private static final int RUNS = 5;
        <i>31</i>&nbsp; private static final String LOG_HEADER =
        <i>32</i>&nbsp; &quot;isAlive, hasCollided, currentDirection, nextDirection, remainingPellets, score&quot;;
        <i>33</i>&nbsp; private static final String BASE_LOG_DIRECTORY = &quot;behavioral-analysis/logs&quot;;
        <i>34</i>&nbsp;
        <i>35</i>&nbsp; private static File logDirectory;
        <i>36</i>&nbsp;
        <i>37</i>&nbsp; private Launcher launcher;
        <i>38</i>&nbsp;
        <i>39</i>&nbsp; /**
        <i>40</i>&nbsp; * Sets and cleans up directory for storing logs.
        <i>41</i>&nbsp; *
        <i>42</i>&nbsp; * @throws IOException when file/folder can&#39;t be created.
        <i>43</i>&nbsp; */
        <i>44</i>&nbsp; @BeforeAll
        <i>45</i>&nbsp; static void setUpLogging() throws IOException {
        <b class="nc"><i>46</i>&nbsp; logDirectory = new File(BASE_LOG_DIRECTORY,
            Long.toString(System.currentTimeMillis()));</b>
        <b class="nc"><i>47</i>&nbsp; if (!logDirectory.exists() &amp;&amp; !logDirectory.mkdirs()) {</b>
        <b class="nc"><i>48</i>&nbsp; throw new IOException(&quot;Directory `behavioral-analysis/` could not be created.&quot;);</b>
        <i>49</i>&nbsp; }
        <i>50</i>&nbsp; }
        <i>51</i>&nbsp;
        <i>52</i>&nbsp; /**
        <i>53</i>&nbsp; * Launch the user interface.
        <i>54</i>&nbsp; */
        <i>55</i>&nbsp; @BeforeEach
        <i>56</i>&nbsp; void setUpGame() {
        <b class="nc"><i>57</i>&nbsp; launcher = new Launcher();</b>
        <b class="nc"><i>58</i>&nbsp; launcher.launch();</b>
        <i>59</i>&nbsp; }
        <i>60</i>&nbsp;
        <i>61</i>&nbsp; /**
        <i>62</i>&nbsp; * Quit the user interface when we&#39;re done.
        <i>63</i>&nbsp; *
        <i>64</i>&nbsp; * @throws IOException when log writer cannot be closed.
        <i>65</i>&nbsp; */
        <i>66</i>&nbsp; @AfterEach
        <i>67</i>&nbsp; void tearDown() throws IOException {
        <b class="nc"><i>68</i>&nbsp; launcher.dispose();</b>
        <i>69</i>&nbsp; }
        <i>70</i>&nbsp;
        <i>71</i>&nbsp; /**
        <i>72</i>&nbsp; * Basic fuzzer implementation.
        <i>73</i>&nbsp; *
        <i>74</i>&nbsp; * @param repetitionInfo repeated test information
        <i>75</i>&nbsp; * @throws IOException when the log write created.
        <i>76</i>&nbsp; */
        <i>77</i>&nbsp; @RepeatedTest(RUNS)
        <i>78</i>&nbsp; void fuzzerTest(RepetitionInfo repetitionInfo) throws IOException {
        <b class="nc"><i>79</i>&nbsp; Game game = launcher.getGame();</b>
        <b class="nc"><i>80</i>&nbsp; Direction chosen = Direction.EAST;</b>
        <i>81</i>&nbsp;
        <b class="nc"><i>82</i>&nbsp; String logFileName = &quot;log_&quot; + repetitionInfo.getCurrentRepetition() +
            &quot;.txt&quot;;</b>
        <b class="nc"><i>83</i>&nbsp; File logFile = new File(logDirectory, logFileName);</b>
        <i>84</i>&nbsp;
        <b class="nc"><i>85</i>&nbsp; try (BufferedWriter logWriter = new BufferedWriter(new OutputStreamWriter(</b>
        <i>86</i>&nbsp; new FileOutputStream(logFile, true), StandardCharsets.UTF_8))) {
        <i>87</i>&nbsp;
        <b class="nc"><i>88</i>&nbsp; logWriter.write(LOG_HEADER);</b>
        <i>89</i>&nbsp;
        <i>90</i>&nbsp; try {
        <b class="nc"><i>91</i>&nbsp; game.start();</b>
        <i>92</i>&nbsp;
        <b class="nc"><i>93</i>&nbsp; while (game.isInProgress()) {</b>
        <b class="nc"><i>94</i>&nbsp; chosen = getRandomDirection();</b>
        <i>95</i>&nbsp;
        <b class="nc"><i>96</i>&nbsp; log(logWriter, chosen);</b>
        <b class="nc"><i>97</i>&nbsp; game.getLevel().move(game.getPlayers().get(0), chosen);</b>
        <i>98</i>&nbsp; }
        <b class="nc"><i>99</i>&nbsp; } catch (RuntimeException e) {</b>
        <i>100</i>&nbsp; // Runtime exceptions should not stop the execution of the fuzzer
        <i>101</i>&nbsp; } finally {
        <b class="nc"><i>102</i>&nbsp; log(logWriter, chosen);</b>
        <b class="nc"><i>103</i>&nbsp; game.stop();</b>
        <b class="nc"><i>104</i>&nbsp; }</b>
        <b class="nc"><i>105</i>&nbsp; }</b>
        <i>106</i>&nbsp; }
        <i>107</i>&nbsp;
        <i>108</i>&nbsp; private Direction getRandomDirection() {
        <b class="nc"><i>109</i>&nbsp; return Direction.values()[new Random().nextInt(Direction.values().length)];</b>
        <i>110</i>&nbsp; }
        <i>111</i>&nbsp;
        <i>112</i>&nbsp; private void log(BufferedWriter logWriter, Direction chosen) throws IOException {
        <b class="nc"><i>113</i>&nbsp; Game game = launcher.getGame();</b>
        <b class="nc"><i>114</i>&nbsp; Player player = game.getPlayers().get(0);</b>
        <i>115</i>&nbsp;
        <b class="nc"><i>116</i>&nbsp; logWriter.write(</b>
        <b class="nc"><i>117</i>&nbsp; String.format(</b>
        <i>118</i>&nbsp; &quot;%n%b %b %s %s %d %d&quot;,
        <b class="nc"><i>119</i>&nbsp; player.isAlive(),</b>
        <b class="nc"><i>120</i>&nbsp; (player.getKiller() != null),</b>
        <b class="nc"><i>121</i>&nbsp; player.getDirection(),</b>
        <i>122</i>&nbsp; chosen,
        <b class="nc"><i>123</i>&nbsp; game.getLevel().remainingPellets(),</b>
        <b class="nc"><i>124</i>&nbsp; player.getScore()</b>
        <i>125</i>&nbsp; )
        <i>126</i>&nbsp; );
        <i>127</i>&nbsp; }
        <i>128</i>&nbsp;}
    </div>
</div>

<div class="footer">

    <div style="float:right;">generated on 2023-04-25 16:45</div>
</div>
</body>
</html>
